import {
  getProjectCssPath,
  getProjectImgPath,
  getProjectJsPath,
  getProjectPath,
  getProjectResPath,
} from "../render";
import { fileStructureInterface } from "../server";
const path = require("path");
const fs = require("fs");

export const quoteCss = (
  indexFile: string,
  cssContent: string,
  cssFileName: string
): string => {
  cssFileName = cssFileName.endsWith(".css")
    ? cssFileName
    : cssFileName + ".css";
  let quote = `<link rel="stylesheet" type="text/css" href="res/css/${cssFileName}" />`;
  if (indexFile.indexOf("<head>") === -1) {
    throw new Error("cannot find head ");
  }
  if (indexFile.indexOf(quote) > -1) return;
  createCssFile(cssContent, cssFileName);
  return indexFile.replace("</head>", `\n\t${quote}\n</head>`);
};

export const createCssFile = (cssContent: string, cssFileName: string) => {
  const imgPath = path.resolve(getProjectCssPath(), cssFileName);
  console.log(imgPath, "img");

  fs.writeFileSync(imgPath, cssContent);
};

export const copyAndThrowErrIfExist = (from: string, to: string) => {
  if (fs.existsSync(to)) throw new Error("file already exist!");
  fs.copyFileSync(from, to);
};

export const copyIfNotExist = (from: string, to: string) => {
  if (fs.existsSync(to)) return;
  fs.copyFileSync(from, to);
};

export const appendToBody = (indexFile: string, text: string) => {
  if (indexFile.indexOf("</body>") === -1) {
    throw new Error("cannot find head ");
  }
  if (indexFile.indexOf(text) > -1) return indexFile;

  return indexFile.replace("</body>", `\n\t${text}\n</body>`);
};

export const updateStructure = (): void => {
  const css = fs.readdirSync(path.resolve(getProjectResPath(), "css"));
  const js = fs.readdirSync(path.resolve(getProjectResPath(), "js"));
  const img = fs.readdirSync(path.resolve(getProjectResPath(), "img"));
  const html = fs
    .readdirSync(getProjectPath())
    .filter((val) => val.endsWith(".html") || val.endsWith(".htm"));
  const Store = require("electron-store");
  const store = new Store();
  let structure: fileStructureInterface = {
    html: [],
    js: [],
    css: [],
    img: [],
  };
  let rawStructure: fileStructureInterface = {
    html: [],
    js: [],
    css: [],
    img: [],
  };

  structure.html = html.map((val) => path.resolve(getProjectPath(), val));
  structure.js = js.map((val) => path.resolve(getProjectJsPath(), val));
  structure.css = css.map((val) => path.resolve(getProjectCssPath(), val));
  structure.img = img.map((val) => path.resolve(getProjectImgPath(), val));
  store.set("projectStructure", JSON.stringify(structure));

  rawStructure.html = html;
  rawStructure.css = css;
  rawStructure.js = js;
  rawStructure.img = img;
  store.set("projectStructureRaw", JSON.stringify(rawStructure));
};

export const copyDir = (from: string, to: string) => {
  const list = fs.readdirSync(from);
  list.forEach((el: string) => {
    const subFrom = path.join(from, el);
    const subTo = path.join(to, el);
    if (fs.statSync(subFrom).isDirectory()) {
      // if (!fs.existsSync(subFrom)) fs.mkdirSync(subTo);
      try {
        fs.mkdirSync(subTo);
      } catch (err) {
        console.log(err);
      }
      copyDir(subFrom, subTo);
    } else {
      fs.copyFileSync(subFrom, subTo);
    }
  });
};

export const delDir = (dir: string) => {
  if (!fs.existsSync(dir)) {
    console.log(dir);
    throw new Error("dir does not exist!");
  }
  const list = fs.readdirSync(dir);
  list.forEach((el: string) => {
    const subDir = path.join(dir, el);
    if (fs.statSync(subDir).isDirectory()) {
      delDir(subDir);
    } else {
      fs.unlinkSync(subDir);
    }
  });
  fs.rmdirSync(dir);
};

//gain file affix from file path
export const fileType = (
  filePath: string
): "js" | "css" | "html" | "htm" | "less" | "download" => {
  return filePath.substring(filePath.lastIndexOf(".") + 1) as unknown as
    | "js"
    | "css"
    | "html"
    | "htm"
    | "less"
    | "download";
};

export const isDirectory = (src: string) => {
  return fs.statSync(src).isDirectory();
};
